<?php

namespace app\shop\controller\setting;

use app\shop\controller\Controller;
use app\shop\model\settings\Setting as SettingModel;
use app\common\enum\settings\DeliveryTypeEnum;

/**
 * 商城设置控制器
 */
class Store extends Controller
{
    /**
     * 商城设置
     */
    public function index()
    {
        if ($this->request->isGet()) {
            return $this->fetchData();
        }
        $model = new SettingModel;
        $data = $this->request->param();
        // 配送方式排序
        sort($data['checkedCities']);
        $arr = [
            'name' => $data['name'],
            'delivery_type' => $data['checkedCities'],
            'kuaidi100' => ['customer' => $data['customer'], 'key' => $data['key']],
            'supplier_cash' => $data['supplier_cash'],
            'commission_rate' => $data['commission_rate'],
            'add_audit' => $data['add_audit'],
            'edit_audit' => $data['edit_audit'],
            'supplier_image' => $data['supplier_image'],
            'sms_open' => $data['sms_open'],
            'supplier_logo' => $data['supplier_logo'],
            'is_get_log' => $data['is_get_log'],
            'policy' => ['service' => $data['service'], 'privacy' => $data['privacy']],
            'store_open' => $data['store_open'],
            'namemobile' => $data['namemobile'],
            'yufeiyufu' => $data['yufeiyufu'],
            'baoxianbi' => $data['baoxianbi'],
            'qinbugonglishu' => $data['qinbugonglishu'],
            'shouye_image' => $data['shouye_image'], 
            'erweima_image' => $data['erweima_image'],
            'bobaode' => $data['bobaode'],
            'package_pt' => $data['package_pt'],
            'package_zy' =>  $data['package_zy'],
            'package_hh' =>  $data['package_hh'],
            'package_mt' =>   $data['package_mt'],
        ];
        if ($model->edit('store', $arr)) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }
	
	public function jieshouwenjian()
    {
		
		
		
  // 2. 检查是否有文件上传
    if (!isset($_FILES['file'])) {
        return $this->renderError('未检测到上传文件');
    }

    // 3. 目录准备
    $target_dir = "uploads/mp3/";
    if (!is_dir($target_dir)) {
        if (!mkdir($target_dir, 0777, true)) {
            return $this->renderError('服务器目录创建失败');
        }
    }

    // 4. 文件信息
    $file      = $_FILES['file'];
    $fileType  = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
    $maxSize   = 12 * 1024 * 1024; // 12MB

    // 5. 类型校验
    if ($fileType !== 'mp3') {
        return $this->renderError('只允许上传MP3文件');
    }

    // 6. 大小校验
    if ($file['size'] > $maxSize) {
        return $this->renderError('文件大小不能超过12MB');
    }

    // 7. 强制文件名为 ForElise.mp3
    $target_file = $target_dir . 'ForElise.mp3';

    // 8. 移动文件
    if (!move_uploaded_file($file['tmp_name'], $target_file)) {
        return $this->renderError('上传失败，服务器写入错误');
    }

    // 9. 返回成功，返回相对路径
    $sdasd = $target_file;
    return $this->renderSuccess('上传成功', compact('sdasd'));
    }
// }
      // return $this->renderSuccess('', compact('sdasd'));
    // }

    /**
     * 获取商城配置
     */
    public function fetchData()
    {
        $vars['values'] = SettingModel::getItem('store');
        $all_type = DeliveryTypeEnum::data();
        return $this->renderSuccess('', compact('vars', 'all_type'));
    }

}
